package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 检查项管理
 */
@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;

    /**
     * 添加检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result addCheck(@RequestBody CheckItem checkItem){
        try {
            log.info("添加检查组成功");
            checkItemService.addCheck(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result deleteCheck(int id){
        try {
            log.info("删除检查组成功");
            checkItemService.deleteCheck(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据检查组查询检查项
     * @param checkgroupId
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(int checkgroupId){
        try {
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId));
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 修改检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestBody CheckItem checkItem){
        try {
            log.info("修改检查项成功");
            checkItemService.update(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 分页查询
     */
    @PostMapping("/findPage")
        public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查组成功");
        return checkItemService.findPage(queryPageBean);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("findAll")
    public Result findAll(){
        log.info("查询所有检查项成功");
        return checkItemService.findAll();
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id){
        try {
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemService.findById(id));
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
